package zcw.com.lib_leet_code;

import zcw.com.lib_leet_code.bean.ListNode;
import zcw.com.lib_leet_code.util.Util;

/**
 * Created by 朱城委 on 2021/7/8.<br><br>
 *
 * 环形链表
 */
public class Topic141 {

    public static void main(String[] args) {
        Topic141 instance = new Topic141();

        int[] array = new int[] {3,2,0,-4};
        ListNode head = Util.createList(array);
        System.out.println(instance.hasCycle(head));
    }

    public boolean hasCycle(ListNode head) {
        if(head == null) {
            return false;
        }

        ListNode low = head;
        ListNode quick = head;

        while (low != null && quick != null && quick.next != null) {
            low = low.next;
            quick = quick.next.next;

            if(low == quick) {
                return true;
            }
        }

        return false;
    }
}
